﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SelfService.Helpers;
using SelfService.Model.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;

namespace SelfService.Controllers
{
    public partial class SysClientTaskController : BaseApiController
    {
        [HttpGet("GetTask")]
        [Public]
        public async Task<Result<object>> GetTask()
        {
            var client = await this.GetClientAsync();
            if (client == null)
            {
                return new Result<object>()
                {
                    Code = 1,
                    Message = "客户端不存在"
                };
            }

            var task = await DC.Set<SysClientTask>()
                .OrderBy(e => e.CreateTime)
                .FirstOrDefaultAsync(e => e.Status == ClientTaskStatus.等待执行);
            if (task == null)
            {
                return new Result<object>()
                {
                    Code = 2,
                    Message = "暂无可执行任务"
                };
            }
            task.Status = ClientTaskStatus.执行完成;
            task.UpdateTime = DateTime.Now;
            await DC.SaveChangesAsync();
            return new Result<object>()
            {
                Data = task
            };
        }

        [HttpGet("GetTask/{mode}")]
        [Public]
        public async Task<Result<object>> GetTask(string mode)
        {
            var client = await this.GetClientAsync();
            if (client == null)
            {
                return new Result<object>()
                {
                    Code = 1,
                    Message = "客户端不存在"
                };
            }

            if (mode.ToLower() == "web")
            {
                var task = await DC.Set<SysClientTask>()
                    .OrderBy(e => e.CreateTime)
                    .FirstOrDefaultAsync(e =>
                        e.ClientId == client.Entity.ID &&
                        e.Status == ClientTaskStatus.等待执行 && (
                            e.Action == ClientTaskActionEnum.关闭 || 
                            e.Action == ClientTaskActionEnum.刷新 || 
                            e.Action == ClientTaskActionEnum.关机 || 
                            e.Action == ClientTaskActionEnum.重启));
                if (task == null)
                {
                    return new Result<object>()
                    {
                        Code = 2,
                        Message = "暂无可执行任务"
                    };
                }
                task.Status = ClientTaskStatus.执行完成;
                task.UpdateTime = DateTime.Now;
                await DC.SaveChangesAsync();
                return new Result<object>()
                {
                    Data = task
                };
            }
            else
            {
                var task = await DC.Set<SysClientTask>()
                    .OrderBy(e => e.CreateTime)
                    .FirstOrDefaultAsync(e => 
                        e.ClientId == client.Entity.ID &&
                        e.Status == ClientTaskStatus.等待执行 && (
                        e.Action == ClientTaskActionEnum.强制关闭 || 
                        e.Action == ClientTaskActionEnum.强制关机 || 
                        e.Action == ClientTaskActionEnum.强制刷新 || 
                        e.Action == ClientTaskActionEnum.强制重启));
                if (task == null)
                {
                    return new Result<object>()
                    {
                        Code = 2,
                        Message = "暂无可执行任务"
                    };
                }
                task.Status = ClientTaskStatus.执行完成;
                task.UpdateTime = DateTime.Now;
                await DC.SaveChangesAsync();
                return new Result<object>()
                {
                    Data = task
                };
            }

        }
    }
}
